<template>
  <el-dialog
    v-model="props.knowledgeDialog"
    :title="props.knowledgeForm?.title + '知识库'"
    width="25%"
    :show-close="false"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
      @submit.native.prevent
    >
      <el-form-item label="知识库名称" prop="deviceTypeName">
        <el-input
          v-model="ruleForm.deviceTypeName"
          placeholder="请输入知识库名称"
          maxlength="12"
          show-word-limit
        />
      </el-form-item>
      <div style="text-align: right">
        <el-button @click="quxiao(ruleFormRef)"> 取消 </el-button>
        <el-button
          type="primary"
          @click="queren(ruleFormRef)"
          :loading="loading"
          >确认</el-button
        >
      </div>
    </el-form>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElTree, FormInstance, FormRules } from 'element-plus'
import { ref, reactive, watch } from 'vue'
const props = defineProps({
  knowledgeDialog: Boolean,
  knowledgeForm: Object
})
const emit = defineEmits(['cancelKnow', 'subKnow'])
const formSize = ref('default')
const loading = ref(false)
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref({
  deviceTypeName: ''
})
const rules = reactive<FormRules>({
  deviceTypeName: [
    { required: true, message: '请输入知识库名称', trigger: 'blur' }
  ]
})

const quxiao = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emit('cancelKnow')
}
const queren = (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = false
      emit('subKnow', ruleForm.value.deviceTypeName)
    } else {
      loading.value = false
    }
  })
}

watch(
  () => props.knowledgeDialog,
  () => {
    ruleForm.value.deviceTypeName = props.knowledgeForm?.val
  }
)
</script>
<style scoped lang="scss"></style>
